package com.fengpanlong.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fengpanlong.entity.Employee;
import com.fengpanlong.entity.EmployeeProject;
import com.fengpanlong.entity.Gogal;
import com.fengpanlong.entity.Task;
import com.fengpanlong.service.EmployeeService;
import com.fengpanlong.service.GogalService;
import com.fengpanlong.service.MessageService;
import com.fengpanlong.util.Form;
import com.fengpanlong.util.GogalHelp;
import com.fengpanlong.util.StringUtil;

public class GogalServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getMethod();
		String m= request.getParameter("_m");
		if("GET".equalsIgnoreCase(method)) {
			if("gotoTaskJsp".equals(m)) {
				gotoTaskJsp(request,response);
			} else if("editTask".equals(m)) {
				editTask(request,response);
			} else if("newTask".equals(m)) {
				newTask(request,response);
			} else if("newGogal".equals(m)) {
				newGogal(request,response);
			} else if("editGogal".equals(m)) {
				editGogal(request,response);
			} else if("delGogal".equals(m)) {
				delGogal(request,response);
			} else if("delTask".equals(m)) {
				delTask(request,response);
			} else if("setNewRate".equals(m)) {
				setNewRate(request,response);
			} else {
				showGogals(request,response);
			}
		} else {
			if("editTask".equals(m)) {
				editTask(request,response);
			} else if("saveTask".equals(m)) {
				saveTask(request,response);
			} else if("saveGogal".equals(m)) {
				saveGogal(request,response);
			} else if("editGogal".equals(m)) {
				editGogal(request,response);
			}
		}
	}
	/**
	 * ajax设置新的完成度
	 * @param request
	 * @param response
	 */
	private void setNewRate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		Task task = GogalService.findTaskByTaskId(id);
		Form.saveParam(request, task);
		GogalService.updateTask(task);
		
		String gogalId = request.getParameter("gogalId");
		GogalHelp gogalHelp = new GogalHelp(gogalId);
		int rate = gogalHelp.getRate();
		response.setContentType("application/json;charset=UTF-8");
		String str="{\"rate\":"+rate+"}";
		PrintWriter out = response.getWriter();
		out.print(str);
		out.flush();
		out.close();
	}
	private void delTask(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String taskId = request.getParameter("taskId");
		Task task = GogalService.findTaskByTaskId(taskId);
		GogalService.delTaskByTaskId(taskId);
		String content =" 删除了名字为"  + task.getName() + "</a>的任务。";
		MessageService.sendMessage(request,content);
		response.sendRedirect("gogal.jspx?_m=gotoTaskJsp&id=" + task.getGogalId());	
	}
	/**
	 * 删除项目
	 * @param request
	 * @param response
	 */
	private void delGogal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String gogalId = request.getParameter("gogalId");
		GogalService.delGogalByGogalId(gogalId);
		showGogals(request, response);
	}
	/**
	 * 编辑目标
	 * @param request
	 * @param response
	 */
	private void editGogal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String method = request.getMethod();
		if("GET".equalsIgnoreCase(method)) {
			String gogalId = request.getParameter("gogalId");
			Gogal gogal = GogalService.findGogalByGogalId(gogalId);
			request.setAttribute("gogal", gogal);
			goTojsp(request, response, "editGogal.jsp");
		} else {
			String gogalId = request.getParameter("gogalId");
			Gogal gogal = GogalService.findGogalByGogalId(gogalId);
			Form.saveParam(request, gogal);
			
			GogalService.updateGogal(gogal);
			String content =" 修改了名字为" + "<a href = \"gogal.jspx\">" + gogal.getName() + "</a>的目标。";
			MessageService.sendMessage(request,content,gogal.getCreatetime());
			showGogals(request, response);
		}
	}
	/**
	 * 保存目标
	 * @param request
	 * @param response
	 */
	private void saveGogal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		Gogal gogal = new Gogal();
		Form.saveParam(request, gogal);
		
		gogal.setId(StringUtil.getId());
		gogal.setCreatetime(StringUtil.formateTime());
		gogal.setEmployeeId(ep.getEmployeeId());
		gogal.setProjectId(ep.getProjectId());
		GogalService.saveGogal(gogal);
		String content =" 创建了名字为" + "<a href = \"gogal.jspx\">" + gogal.getName() + "</a>的目标。";
		MessageService.sendMessage(request,content,gogal.getCreatetime());
		showGogals(request, response);
	}
	/**
	 * 添加新目标
	 * @param request
	 * @param response
	 */
	private void newGogal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		goTojsp(request, response, "newGogal.jsp");
		
	}
	/**
	 * 保存任务
	 * @param request
	 * @param response
	 */
	private void saveTask(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String gogalId = request.getParameter("gogalId");
		String employeeId = request.getParameter("employeeId");
		
		Task task = new Task();
		Form.saveParam(request, task);
		
		task.setId(StringUtil.getId());
		task.setCreatetime(StringUtil.formateTime());
		task.setGogalId(gogalId);
		task.setEmployeeId(employeeId);
		task.setRate("0");
		GogalService.saveTask(task);
		String content =" 创建了名字为" + "<a href = \"gogal.jspx?_m=gotoTaskJsp&id=" + gogalId + "\">" + task.getName() + "</a>的任务。";
		MessageService.sendMessage(request,content,task.getCreatetime());
		response.sendRedirect("gogal.jspx?_m=gotoTaskJsp&id=" + gogalId);
	}
	/**
	 * 添加新任务
	 * @param request
	 * @param response
	 */
	private void newTask(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String gogalId = request.getParameter("gogalId");
		request.setAttribute("gogalId", gogalId);
		List<Employee> employeeList = EmployeeService.findEmployeesForList();
		request.setAttribute("employeeList", employeeList);
		goTojsp(request, response, "newTask.jsp");
	}
	/**
	 * 编辑任务
	 * @param request
	 * @param response
	 */
	private void editTask(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String method = request.getMethod();
		if("GET".equalsIgnoreCase(method)) {
			String taskId = request.getParameter("taskId");
			Task task = GogalService.findTaskByTaskId(taskId);
			request.setAttribute("task", task);
			List<Employee> employeeList = EmployeeService.findEmployeesForList();
			request.setAttribute("employeeList", employeeList);
			goTojsp(request, response, "editTask.jsp");
		} else {
			String taskId = request.getParameter("taskId");
			Task task = GogalService.findTaskByTaskId(taskId);
			Form.saveParam(request, task);
			GogalService.updateTask(task);
			String content =" 修改了名字为" + "<a href = \"gogal.jspx?_m=gotoTaskJsp&id=" + task.getGogalId() + "\">" + task.getName() + "</a>的任务。";
			MessageService.sendMessage(request,content);
			response.sendRedirect("gogal.jspx?_m=gotoTaskJsp&id=" + task.getGogalId());	
		}
	}
	/**
	 * 去对应任务页面
	 * @param request
	 * @param response
	 */
	private void gotoTaskJsp(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gogalId = request.getParameter("id");
		GogalHelp gogalHelp = new GogalHelp(gogalId);
		request.setAttribute("gogalHelp",gogalHelp);
		goTojsp(request, response, "task.jsp");
	}
	/**
	 * 显示目标界面
	 * @param request
	 * @param response
	 */
	private void showGogals(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		List<Gogal> gogalList = GogalService.findGogalsByProjectId(ep.getProjectId());
		List<GogalHelp> gogalHelpList = new ArrayList<GogalHelp>();
		GogalHelp gogalHelp = null;
		for(Gogal g : gogalList) {
			gogalHelp = new GogalHelp(g.getId());
			gogalHelpList.add(gogalHelp);
		}
		request.setAttribute("gogalHelpList", gogalHelpList);
		goTojsp(request, response, "gogal.jsp");
	}
	/**
	 * 去对应jsp页面
	 * @param request
	 * @param response
	 * @param jspName
	 * @throws ServletException
	 * @throws IOException
	 */
	private void goTojsp(HttpServletRequest request,
			HttpServletResponse response, String jspName) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/" + jspName).forward(request, response);
	}
}
